﻿/* Open source distributed without warranties of any kind. */

namespace Transcriber.Plugins
{
    #region Using
    using System;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;
    #endregion

    /// <summary>
    /// Dialog box to allow a user to experiment with regular expressions.
    /// </summary>
    /// <remarks>
    /// Primarily intended as a utility for rules file authors, but useful
    /// for anyone wishing to learn about regular expressions.
    /// </remarks>
    public partial class frmRegexTest : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="frmRegexTest"/> class.
        /// </summary>
        public frmRegexTest()
        {
            InitializeComponent();
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            try
            {
                Regex regex = new Regex(this.txtRegex.Text, RegexOptions.IgnoreCase | RegexOptions.Multiline);
                this.txtResult.Text = regex.Replace(this.txtSource.Text, this.txtReplacement.Text);

                foreach (Match match in regex.Matches(this.txtSource.Text))
                {
                    ListViewItem item = new ListViewItem(new string[] { match.Index.ToString(), match.Length.ToString(), match.Value });
                    this.lvMatches.Items.Add(item);
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error");
            }
        }
    }
}
